<template>
    <div>
        <div style="margin: 15px 0;">
            <el-input placeholder="请输入内容" v-model="parameter.query" class="input-with-select"
                style="width: 54%;margin-right: 10px;">

                <el-button slot="append" icon="el-icon-search" @click="apply"></el-button>
            </el-input>
            <el-button type="primary" @click="$router.push({
                path: '/add',
                query: {
                },
            })">添加商品</el-button>
            <el-button type="primary">导出excel表格</el-button>
        </div>
        <el-table :data="goodsData" :border="true">

            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column label="商品名称" prop="goods_name"> </el-table-column>
            <el-table-column label="商品价格" prop="goods_price"> </el-table-column>
            <el-table-column label="商品重量" prop="goods_weight"> </el-table-column>
            <el-table-column label="创建时间" prop="path">
                <template slot-scope="scope">
                    {{  scope.row.upd_time | trmes  }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" circle @click="$router.push({
                        path: '/add',
                        query: {
                            obj: JSON.stringify(scope.row)
                        },
                    })"></el-button>
                    <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.goods_id)" circle></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页功能 -->
        <el-pagination @size-change="ChangeSize" @current-change="ChangePages" :current-page="parameter.pagenum"
            :page-sizes="[1, 3, 5, 10]" :page-size="parameter.pagesize" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </div>
</template>

<script>
import { getGoogs, del } from '@/http/goods'
export default {
    data() {
        return {
            SearchGoods: '',
            goodsData: [],
            parameter: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            total: 0

        }
    },
    methods: {
        //重新渲染页面
        async apply() {
            let res = await getGoogs(this.parameter)
            this.goodsData = res.data.goods
            this.total = res.data.total

        },
        //页数变化
        ChangePages(v) {
            this.parameter.pagenum = v
            this.apply()
        },
        //条数发生变化
        ChangeSize(v) {
            this.parameter.pagesize = v
            this.apply()
        },
        //删除商品
        async del(id) {

            let res = await del(id * 1)
            this.apply()
        }

    },
    created() {
        this.apply()
    }
}
</script>

<style lang="scss" scoped>
</style>